package com.i2works.smartluolong.utils.basecommon;


import com.i2works.smartluolong.utils.common.BaseUtils;
import com.i2works.smartluolong.utils.exception.DAOException;
import com.i2works.smartluolong.utils.exception.ServiceException;
import com.xiaoleilu.hutool.util.CollectionUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 公共Service实现类
 *
 * @author Liu Yutao <koal@vip.qq.com>
 * @date 2017/12/24 16:33
 * @since 抽象的Service实现, 专门用于继承
 */
public abstract class BaseCommonServiceImpl<Q, D> implements BaseCommonService<Q, D> {

    private BaseCommonDAO<Q, D> baseCommonDAO;

    /**
     * 注入DAO
     */
    @Autowired
    public void setDAO(BaseCommonDAO<Q, D> baseCommonDAO) {
        this.baseCommonDAO = baseCommonDAO;
    }

    /**
     * 查询列表
     *
     * @param query 查询条件
     */
    @Override
    public List<D> queryList(Q query) throws ServiceException {
        try {
            return baseCommonDAO.queryList(query);
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }

    /**
     * 查询列表数量
     *
     * @param query 查询条件
     */
    @Override
    public Integer queryCount(Q query) throws ServiceException {
        try {
            return baseCommonDAO.queryCount(query);
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }

    /**
     * 查询单个对象
     *
     * @param query 查询条件
     */
    @Override
    public D query(Q query) throws ServiceException {
        try {
            return baseCommonDAO.query(query);
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }

    /**
     * 查询检测状态
     *
     * @param query 查询条件
     */
    @Override
    public Integer queryCheck(Q query) throws ServiceException {
        try {
            return baseCommonDAO.queryCheck(query);
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }

    /**
     * 保存和更新（根据对象ID判断）
     *
     * @param d 保存的对象
     */
    @Transactional(rollbackFor = ServiceException.class)
    @Override
    public void saveOrUpdate(D d) throws ServiceException {
        try {
            Integer idValue = BaseUtils.getFieldValue(d, "id", Integer.class);
            String uuidValue = BaseUtils.getFieldValue(d, "uuid", String.class);
            if (idValue != null || StringUtils.isNotBlank(uuidValue)) {
                baseCommonDAO.update(d);
            } else {
                baseCommonDAO.save(d);
            }
        } catch (Exception e) {
            throw new ServiceException(e);
        }
    }

    /**
     * 保存
     *
     * @param d 保存的对象
     */
    @Transactional(rollbackFor = ServiceException.class)
    @Override
    public void save(D d) throws ServiceException {
        try {
            baseCommonDAO.save(d);
        } catch (Exception e) {
            throw new ServiceException(e);
        }
    }


    /**
     * 批量保存
     *
     * @param list 保存的对象集合
     */
    @Transactional(rollbackFor = ServiceException.class)
    @Override
    public void saveBatch(List<D> list) throws ServiceException {
        try {
            if (CollectionUtil.isNotEmpty(list)) {
                for (D entity : list) {
                    baseCommonDAO.save(entity);
                }
            }
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }


    /**
     * 更新
     *
     * @param d 更新的对象
     */
    @Transactional(rollbackFor = ServiceException.class)
    @Override
    public void update(D d) throws ServiceException {
        try {
            baseCommonDAO.update(d);
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }

    /**
     * 批量更新
     *
     * @param list 更新的对象集合
     */
    @Transactional(rollbackFor = ServiceException.class)
    @Override
    public void updateBatch(List<D> list) throws ServiceException {
        try {
            if (CollectionUtil.isNotEmpty(list)) {
                for (D entity : list) {
                    baseCommonDAO.update(entity);
                }
            }
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }


    /**
     * 更新状态
     *
     * @param d 更新对象
     * @link basecommon.BaseCommonServiceImpl#update
     */
    @Transactional(rollbackFor = ServiceException.class)
    @Override
    public void updateStatus(D d) throws ServiceException {
        try {
            baseCommonDAO.update(d);
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }


    /**
     * 批量更新状态
     *
     * @param map 更新条件
     */
    @Transactional(rollbackFor = ServiceException.class)
    @Override
    public void updateBatchStatus(Map map) throws ServiceException {
        try {
            baseCommonDAO.updateBatchStatus(map);
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }

    /**
     * 单个删除
     *
     * @param id 删除编号
     */
    @Transactional(rollbackFor = ServiceException.class)
    @Override
    public void delete(Integer id) throws ServiceException {
        try {
            baseCommonDAO.delete(id);
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }
    /**
     * 单个删除
     *
     * @param uuid 删除编号
     */
    @Transactional(rollbackFor = ServiceException.class)
    @Override
    public void deleteUUID(String uuid) throws ServiceException {
        try {
            baseCommonDAO.deleteUUID(uuid);
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }

    /**
     * 单个删除
     *
     * @param serviceId 实施清单ID
     */
    @Transactional(rollbackFor = ServiceException.class)
    @Override
    public void deleteServiceId(String serviceId) throws ServiceException {
        try {
            baseCommonDAO.deleteServiceId(serviceId);
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }

    /**
     * 批量删除
     *
     * @param idsList 编号集合
     */
    @Transactional(rollbackFor = ServiceException.class)
    @Override
    public void deleteBatch(List<Integer> idsList) throws ServiceException {
        try {
            baseCommonDAO.deleteBatch(idsList);
        } catch (DAOException e) {
            throw new ServiceException(e);
        }
    }
}